package com.cema.manage.project.manage.adRecordSecondary.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.cema.manage.framework.web.controller.BaseController;
import com.cema.manage.framework.web.page.TableDataInfo;
import com.cema.manage.project.enums.TaKuAdPositionEnum;
import com.cema.manage.project.manage.adRecordSecondary.domain.AdRecordSecondary;
import com.cema.manage.project.manage.adRecordSecondary.domain.AdRecordSecondaryVo;
import com.cema.manage.project.manage.adRecordSecondary.service.IAdRecordSecondaryService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.jetbrains.annotations.NotNull;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 副广记录 信息操作处理
 *
 * @author reasahi
 * @date 2024-09-29
 */
@Controller
@RequestMapping("/manage/adRecordSecondary")
public class AdRecordSecondaryController extends BaseController {
    private String prefix = "manage/adRecordSecondary";

    @Autowired
    private IAdRecordSecondaryService adRecordSecondaryService;

    @GetMapping()
    @RequiresPermissions("manage:adRecordSecondary:view")
    public String adRecordSecondary() {
        return prefix + "/adRecordSecondaryUser";
    }

    @GetMapping("/reward")
    @RequiresPermissions("manage:adRecordSecondary:view")
    public String adRecordSecondaryReward(Model model) {
        model.addAttribute("positionCode", TaKuAdPositionEnum.AD_REWARD_POSITION.getKey());
        return prefix + "/adRecordSecondary";
    }

    @GetMapping("{id}")
    @RequiresPermissions("manage:adRecord:view")
    public String adRecordId(@PathVariable("id") Integer id, Model model) {
        model.addAttribute("userId", id);
        return prefix + "/adRecordSecondaryUser";
    }

    @GetMapping("/splash")
    @RequiresPermissions("manage:adRecordSecondary:view")
    public String adRecordSecondarySplash(Model model) {
        model.addAttribute("positionCode", TaKuAdPositionEnum.AD_SPLASH_POSITION.getKey());
        return prefix + "/adRecordSecondary";
    }

    @GetMapping("/interstitial")
    @RequiresPermissions("manage:adRecordSecondary:view")
    public String adRecordSecondaryInterstitial(Model model) {
        model.addAttribute("positionCode", TaKuAdPositionEnum.AD_INTERSTITIAL_POSITION.getKey());
        return prefix + "/adRecordSecondary";
    }

    @GetMapping("/interstitial_full")
    @RequiresPermissions("manage:adRecordSecondary:view")
    public String adRecordSecondaryInterstitialFull(Model model) {
        model.addAttribute("positionCode", TaKuAdPositionEnum.AD_INTERSTITIAL_FULL_POSITION.getKey());
        return prefix + "/adRecordSecondary";
    }

    @GetMapping("/native")
    @RequiresPermissions("manage:adRecordSecondary:view")
    public String adRecordSecondarySplashNative(Model model) {
        model.addAttribute("positionCode", TaKuAdPositionEnum.AD_NATIVE_POSITION.getKey());
        return prefix + "/adRecordSecondary";
    }

    @GetMapping("banner")
    @RequiresPermissions("manage:adRecordSecondary:view")
    public String adRecordSecondaryBanner(Model model) {
        model.addAttribute("positionCode", TaKuAdPositionEnum.AD_BANNER_POSITION.getKey());
        return prefix + "/adRecordSecondary";
    }

    /**
     * 查询副广记录列表
     */
    @RequiresPermissions("manage:adRecordSecondary:list")
    @ResponseBody
    @PostMapping("/list")
    public TableDataInfo list(AdRecordSecondaryVo adRecordSecondaryVo) {
        startPage();
        EntityWrapper<AdRecordSecondary> entityWrapper = getEntityWrapper(adRecordSecondaryVo);
        List<AdRecordSecondary> list = adRecordSecondaryService.selectList(entityWrapper);
        return getDataTable(list);
    }

    /**
     * 查询广告记录列表
     */
    @RequiresPermissions("manage:adRecord:list")
    @ResponseBody
    @RequestMapping("/statistics")
    public JSONObject listStatistics(AdRecordSecondaryVo adRecordSecondaryVo) {
        EntityWrapper<AdRecordSecondary> entityWrapper = getEntityWrapper(adRecordSecondaryVo);
        List<AdRecordSecondary> adRecordList = adRecordSecondaryService.selectList(entityWrapper);
        Map<String, Long> map = new HashMap<>(1);
        long arRealityMoneySum = 0L;
        if (ObjectUtil.isNotNull(adRecordList)) {
            arRealityMoneySum = adRecordList.stream().mapToInt(AdRecordSecondary::getArsRealityMoney).sum();
        }
        map.put("arsRealityMoneySum", arRealityMoneySum);
        return new JSONObject(map);
    }


    @NotNull
    private EntityWrapper<AdRecordSecondary> getEntityWrapper(AdRecordSecondaryVo adRecordSecondaryVo) {
        EntityWrapper<AdRecordSecondary> entityWrapper = new EntityWrapper<>();
        Date start = adRecordSecondaryVo.getStartTime();
        if (ObjectUtil.isNotNull(start)) {
            entityWrapper.between("create_time", start, adRecordSecondaryVo.getEndTime());
        }
        AdRecordSecondary adRecordSecondary = BeanUtil.toBean(adRecordSecondaryVo, AdRecordSecondary.class);
        entityWrapper.setEntity(adRecordSecondary);
        String startGoldCoin = adRecordSecondaryVo.getStartGoldCoin();
        String endGoldCoin = adRecordSecondaryVo.getEndGoldCoin();
        if (StrUtil.isNotEmpty(startGoldCoin) && StrUtil.isNotEmpty(endGoldCoin)) {
            entityWrapper.between("ars_reality_money", startGoldCoin, Integer.parseInt(endGoldCoin) + 1);
        }
        if (StrUtil.isNotEmpty(startGoldCoin)) {
            entityWrapper.ge("ars_reality_money", startGoldCoin);
        }
        if (StrUtil.isNotEmpty(endGoldCoin)) {
            entityWrapper.le("ars_reality_money", endGoldCoin);
        }
        entityWrapper.orderBy("create_time", false);
        return entityWrapper;
    }
}
